package com.codeczx.completeuidemo.recyclerview.progress;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.codeczx.completeuidemo.R;
import com.github.lzyzsd.circleprogress.DonutProgress;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by win10 on 2017/5/6.
 */

class RVAdapter extends RecyclerView.Adapter<RVAdapter.MyViewHolder>{

    public static final String TAG = "codeczx";

    List<String> mList;

    public RVAdapter(List<String> list) {
        this.mList = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.setId(position);
        holder.tv.setText(mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        DonutProgress pg;
        TextView tv;
        int id;

        public MyViewHolder(View itemView) {
            super(itemView);
            pg = (DonutProgress) itemView.findViewById(R.id.pg);
            tv = (TextView) itemView.findViewById(R.id.text);
        }

        public void setId(int id) {
            this.id = id;
        }

        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onProgressEvent(ProgressEvent event){
            if(id == event.id){
                pg.setDonut_progress(event.progress+"");
            }
        }

    }

    @Override
    public void onViewDetachedFromWindow(MyViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
//        Log.i(TAG,"detach:"+holder.toString());
        EventBus.getDefault().unregister(holder);
    }

    @Override
    public void onViewAttachedToWindow(MyViewHolder holder) {
        super.onViewAttachedToWindow(holder);
//        Log.i(TAG,"attach:"+holder.toString());
        EventBus.getDefault().register(holder);
    }
}
